package com.pu.controller.user;

import com.pu.context.BaseContext;
import com.pu.entity.AddressBook;
import com.pu.result.Result;
import com.pu.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Yusc
 * @Date 2023/11/1 16:59
 * @Version
 * @Description C端地址簿管理
 **/
@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "C端地址簿接口")
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * @auther: Yusc
     * @date 2023/11/1 16:59
     * @Description 查询当前登录用户的所有地址信息
     */
    @GetMapping("/list")
    @ApiOperation("查询当前登录用户的所有地址信息")
    public Result<List<AddressBook>> list() {
        List<AddressBook> list = addressBookService.lambdaQuery()
                .eq(AddressBook::getUserId, BaseContext.getCurrentId())
                .list();
        return Result.success(list);
    }

    /**
     * @param addressBook 前端传递过来的参数
     * @auther: Yusc
     * @date 2023/11/1 17:12
     * @Description 新增地址
     */
    @PostMapping
    @ApiOperation("新增地址")
    public Result save(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());  // 设置用户did
        addressBook.setIsDefault(0);    // 默认为：不是默认地址
        addressBookService.save(addressBook);
        return Result.success();
    }

    /**
     * @param id 地址薄的主键id
     * @auther: Yusc
     * @date 2023/11/1 17:20
     * @Description 根据id查询地址 主要用于数据回显
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    /**
     * @param addressBook 前端传递过来的数据
     * @auther: Yusc
     * @date 2023/11/1 17:24
     * @Description 根据id修改地址
     */
    @PutMapping
    @ApiOperation("根据id修改地址")
    public Result update(@RequestBody AddressBook addressBook) {
        log.info("根据id修改地址：要修改的地址数据为:{}", addressBook);
        addressBookService.update(addressBook);
        return Result.success();
    }

    /**
     * @param addressBook 前端传递的数据
     * @auther: Yusc
     * @date 2023/11/1 17:31
     * @Description 设置默认地址
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefault(@RequestBody AddressBook addressBook) {
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

    /**
     * @param id 地址薄id
     * @auther: Yusc
     * @date 2023/11/1 17:41
     * @Description 根据id删除地址
     */
    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result deleteById(Long id) {
        addressBookService.removeById(id);
        return Result.success();
    }

    /**
     * @param
     * @return
     * @auther: Yusc
     * @date 2023/11/1 18:03
     * @Description 查询默认地址
     */
    @GetMapping("default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getDefault() {
        AddressBook addressBook = new AddressBook();
        addressBook.setIsDefault(1);
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.lambdaQuery()
                .eq(AddressBook::getUserId, addressBook.getUserId())
                .eq(AddressBook::getIsDefault, addressBook.getIsDefault())
                .list();

        // 判断用户是否设置默认地址，如果没有就抛出错误信息
        if (list != null && list.size() == 1) {
            return Result.success(list.get(0));
        }

        return Result.error("没有查询到默认地址");
    }
}
